package com.baizhi.controller;


import com.baizhi.annotation.LogAnnotation;
import com.baizhi.entity.Log;
import com.baizhi.service.LogService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("log")
public class LogController {
    @Autowired
    private LogService logService;

    @LogAnnotation(value = "查询所有的日志",type = "selectpage")
    @ResponseBody
    @RequestMapping("selectPage")
    public Map selectPage(Integer page, Integer limit){
        System.out.println("--------------");
        Map m = new HashMap();
        Subject subject = SecurityUtils.getSubject();
//        if(subject.hasRole("superadmin")){
            System.out.println("+++++++++++");
            PageInfo<Log> pageInfo = logService.selectAll(page, limit);
            System.out.println("**************");
            m.put("code",0);
            m.put("msg","");
            m.put("count",pageInfo.getTotal());
            m.put("data",pageInfo.getList());
            return m;
//        }
//        return null;

    }

    @ResponseBody
    @RequestMapping("add")
    public Map add(Log log){
        Map m = new HashMap();
        try {
            logService.insert(log);
            m.put("flag",true);
        } catch (Exception e) {
            e.printStackTrace();
            m.put("flag",true);
        }
        return m;
    }
    @ResponseBody
    @RequestMapping("deletes")
    public Map deletes(Integer[] ids){
        Map m = new HashMap();
        try {
            for (Integer id :ids) {
                logService.deleteByPrimaryKey(id);
            }
            m.put("flag",true);
        } catch (Exception e) {
            e.printStackTrace();
            m.put("flag",true);
        }
        return m;
    }
}
